<template>
  <div class="page-container">
    <div class="add-wrap">
      <div class="header-section">
        <Breadcrumb :titles="titles"></Breadcrumb>
        <div class="title">
          数据月份：
          <el-date-picker
            v-model="select"
            type="month"
            @change="changeMonth"
            :clearable="false"
            placeholder="选择月"
          >
          </el-date-picker>
        </div>
      </div>

      <div class="table-section">
        <el-table
          :data="tableData"
          :header-cell-style="{
            background: '#F2F6FB',
            color: ' #6B767D'
          }"
          height="600"
        >
          <el-table-column
            label="项目名称"
            prop="projectName"
            show-overflow-tooltip
          />
          <el-table-column
            label="本年度计划固定投资(万元)"
            prop="fixedTotal"
            show-overflow-tooltip
          />
          <el-table-column
            label="一季度计划固投(万元)"
            prop="fixedOne"
            show-overflow-tooltip
          />
          <el-table-column
            label="二季度计划固投(万元)"
            prop="fixedTwo"
            show-overflow-tooltip
          />
          <el-table-column
            label="三季度计划固投(万元)"
            prop="fixedThree"
            show-overflow-tooltip
          />
          <el-table-column
            label="四季度计划固投(万元)"
            prop="fixedFour"
            show-overflow-tooltip
          />
          <el-table-column
            label="上月累计完成固投(万元)"
            prop="lastMonthFixedTotalInvestment"
            show-overflow-tooltip
          />
          <el-table-column label="本年度累计完成固投(万元)">
            <template slot-scope="scope">
              <!-- <el-button
                  size="mini"
                  @click="handleEdit(scope.$index, scope.row)">编辑</el-button> -->
              <el-input
                v-model="scope.row.fixedTotalInvestment"
              ></el-input>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="line">
      <el-button @click="saveInfo" class="save-btn"
        >保存</el-button
      >
    </div>
  </div>
</template>

<script>
import Breadcrumb from '@/components/breadcrumb'
import dayjs from 'dayjs'
import {
  queryFixedInfoByMonth,
  addFixedInfo
} from '@/api/dataManagement'

export default {
  props: {
    title: String,
    projectId: String
  },
  components: {
    Breadcrumb
  },
  data() {
    return {
      tableData: [],
      select: ''
    }
  },
  computed: {
    titles() {
      return [this.title]
    }
  },
  mounted() {
    const date = new Date()
    this.select = dayjs(date).format('YYYY-M')
    this.searchInfo()
  },
  methods: {
    searchInfo() {
      const year = this.select.split('-')[0]
      const month = this.select.split('-')[1]
      queryFixedInfoByMonth(year, month)
        .then((res) => {
          if (res.success) {
            this.tableData = res.data
          } else {
            this.$message.error(res.message || '保存失败')
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    saveInfo() {
      const year = this.select.split('-')[0]
      const month = this.select.split('-')[1]
      const params = this.tableData.map((item) => {
        return {
          projectId: item.projectId,
          year: year,
          month: month,
          fixedTotalInvestment: item.fixedTotalInvestment
        }
      })
      addFixedInfo(params)
        .then((res) => {
          if (res.success) {
            this.$message.success('保存成功')
            this.$router.push({
              path: '/dataManagement/projectManagement/fixedTargetMap/list'
            })
          } else {
            this.$message.error(res.message || '保存失败')
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    changeMonth(val) {
      this.select = dayjs(val).format('YYYY-M')
      this.searchInfo()
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/assets/css/table.scss';
.page-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.add-wrap {
  flex: 1;
  overflow: auto;
  padding: 10px 20px;

  .title {
    margin: 15px 0;
  }
}

// .header-section {
//   padding: 10px 20px;
//   flex-shrink: 0;

//   .title {
//     margin: 15px 0;
//   }
// }

.line {
  flex-shrink: 0;
  padding: 10px;
  background: #fff;
  border-top: 1px solid #eee;
  text-align: center;
}

.save-btn {
  background: #497fa3;
  border: 1px solid #497fa3;
  width: 104px;
  color: white;
  font-size: 14px;
  width: 104px;
  height: 34px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px auto;
}
</style>
